<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/13
 * Time: 23:51
 */

namespace app\common\model;

use app\common\lib\Util;
use think\Cache;

/**
 * 逻辑更新模型
 * Class AppLogicVersion
 * @package app\common\mode
 */
class AppLogicVersion extends Base
{
    protected $table = 'wechat_backup_07_18_14_43_00.app_logic_version';

    /**
     * 获取当前档案的所有版本号
     * @param array $map
     * @return bool|array
     */
    public function getAllVersion($map = [])
    {
        if ($map['rid'] && $map['type'] && (in_array($map['type'], [1, 2, 3])))
        {
            $data = false;
            if ($this->cache_on >= 1)
            {
                $data = Cache::get('all_logic_' . $map['type'] . '_' . $map['rid']);
            }

            if ($data === false)
            {
                $where = ['rid' => $map['rid'], 'type' => $map['type']];
                try
                {
                    $data = $this->field('version,time')->where($where)->select();

                    $data = json_decode(json_encode($data, true), true);

                    if ($this->cache_on >= 1)
                    {
                        Cache::set('all_logic_' . $map['type'] . '_' . $map['rid'], $data, 86400 * 7);
                    }
                }
                catch (\Exception $e)
                {
                    $data = [];
                }
            }
            return $data;
        }
        return [];
    }

    /**
     * 获取当前档案的逻辑版本(幸运之城和运势)
     * @param array $map
     * rid int
     * type tinyint
     * time string 运势需要时间 月运如：2019-01 年运如：2019
     * @return bool|int|mixed
     */
    public function getVersion($map = [])
    {
        if ($map['rid'] && $map['type'] && (in_array($map['type'], [1, 2, 3])))
        {
            if ($this->cache_on >= 1 && isset($map['time']))
            {
                $data = Cache::get('logic_' . $map['type'] . '_' . $map['rid'] . '_' . $map['time']);
            } else
            {
                $data = Cache::get('logic_' . $map['type'] . '_' . $map['rid']);
            }

            if ($data === false)
            {
                if (isset($map['time']))
                {
                    $where = ['rid' => $map['rid'], 'type' => $map['type'], 'time' => $map['time']];
                } else
                {
                    $where = ['rid' => $map['rid'], 'type' => $map['type']];
                }
                try
                {
                    $version = $this->field('version')->where($where)->order('id', 'DESC')->findOrFail();

                    $data = $version['version'];
                }
                catch (\Exception $e)
                {
                    $data = 0;
                }
                if ($this->cache_on >= 1 && isset($map['time']))
                {
                    Cache::set('logic_' . $map['type'] . '_' . $map['rid'] . '_' . $map['time'], $data, 86400 * 7);
                } else
                {
                    Cache::set('logic_' . $map['type'] . '_' . $map['rid'], $data, 86400 * 7);
                }
            }
            return $data;
        }
        return 0;
    }

    /**
     * 当前数据使用的逻辑版本写入数据库
     * @param array $data
     * rid int 档案id
     * version int 当前逻辑版本
     * type tinyint 1=>幸运之城，2=>月运，3=>年运
     * @return bool
     */
    public function addVersion($data = [])
    {
        $type_arr = [1, 2, 3];

        if (!empty($data)
            && $data['rid']
            && $data['type']
            && in_array($data['type'], $type_arr)
        )
        {
            $data['create_time'] = time();

            try
            {
                $this->insertGetId($data);

                if ($this->cache_on >= 1 && isset($data['time']))
                {
                    Cache::set('logic_' . $data['type'] . '_' . $data['rid'] . '_' . $data['time'], $data['version'], 86400 * 7);
                } else
                {
                    Cache::set('logic_' . $data['type'] . '_' . $data['rid'], $data['version'], 86400 * 7);
                }
                Cache::rm('all_logic_' . $data['type'] . '_' . $data['rid']);
            }
            catch (\Exception $e)
            {
                \think\Log::write('数据使用的逻辑版本入库失败：' . $e->getMessage() . PHP_EOL . var_export($data, true), 'error', true);
            }
            return true;
        }
        return false;
    }

    /**
     * 更新逻辑使用版本
     * @param array $data
     * rid int 档案id
     * version int 当前逻辑版本
     * type tinyint 1=>幸运之城，2=>月运，3=>年运
     * @return bool
     */
    public function updateVersion($data = [])
    {
        $type_arr = [1, 2, 3];

        if (!empty($data)
            && $data['rid']
            && $data['type']
            && in_array($data['type'], $type_arr)
        )
        {
            $updateData = [
                'version' => $data['version'],
                'update_time' => time(),
            ];
            $updateData['update_time'] = time();

            if (isset($map['time']))
            {
                $map = ['rid' => $data['rid'], 'type' => $data['type'], 'time' => $data['time']];
            } else
            {
                $map = ['rid' => $data['rid'], 'type' => $data['type']];
            }

            try
            {
                $this->where($map)->update($updateData);

                if ($this->cache_on >= 1 && isset($data['time']))
                {
                    Cache::set('logic_' . $data['type'] . '_' . $data['rid'] . '_' . $data['time'], $data['version'], 86400 * 7);
                } else
                {
                    Cache::set('logic_' . $data['type'] . '_' . $data['rid'], $data['version'], 86400 * 7);
                }
                Cache::rm('all_logic_' . $data['type'] . '_' . $data['rid']);
            }
            catch (\Exception $e)
            {
                \think\Log::write('数据使用的逻辑版本入库失败：' . $e->getMessage() . PHP_EOL . var_export($data, true), 'error', true);
            }
            return true;
        }
        return false;
    }
}
